package demo1;

import java.util.Arrays;
import java.util.Scanner;

public class BookList {
   public  Book[] book=new Book[100];
   public int sz=4;

    public BookList() {
        book[0]=new Book("西游记","吴承恩",35);
        book[1]=new Book("水浒传","施耐庵",40);
        book[2]=new Book("三国演义","罗贯中",29);
        book[3]= new Book("红楼梦","曹雪芹",55);
        this.sz=4;
    }

    public void find() {
        System.out.println("请输入要查找的书名");
        Scanner sc=new Scanner(System.in);
        String name1=sc.nextLine();
        for (int i = 0; i <this.sz ; i++) {
            if(this.book[i].name.equals(name1)){
                System.out.println(this.book[i].name+" "+this.book[i].writer+" "+this.book[i].price);
                return;
            }
        }
        System.out.println("没找到");
    }

    public void jieYue() {

        System.out.println("请输入您要借阅的书籍");
        Scanner sc=new Scanner(System.in);
        String name1=sc.nextLine();
        int i=0;
        int flag=-1;
        for (i = 0; i <this.sz ; i++) {
            if(this.book[i].name.equals(name1)){
                flag=1;
                break;
            }
        }
        for (int j = i; j < this.sz-1; j++) {
           this.book[j].name=this.book[j+1].name;
            this.book[j].writer=this.book[j+1].writer;
            this.book[j].price=this.book[j+1].price;

        }
        if(flag==1){
           this.sz--;
        }
        if(flag==-1){
            System.out.println("没找到");
        }
    }
    public void del(){
        System.out.println("请输入您要删除的书籍");
        Scanner sc=new Scanner(System.in);
        String name1=sc.nextLine();
        int i=0;
        int flag=-1;
        for (i = 0; i <this.sz ; i++) {
            if(this.book[i].name.equals(name1)){
                flag=1;
                break;
            }
        }
        for (int j = i; j < this.sz-1; j++) {
            this.book[j].name=this.book[j+1].name;
            this.book[j].writer=this.book[j+1].writer;
            this.book[j].price=this.book[j+1].price;

        }
        if(flag==1){
            this.sz--;
        }
        if(flag==-1){
            System.out.println("没找到");
        }
    }


    public void show() {
        for (int i = 0; i < this.sz; i++) {
            System.out.println(this.book[i].name+" "+this.book[i].writer+" "+this.book[i].price);
        }
    }
    public void guiHuan(){
        this.sz++;
        System.out.println("请输入要归还书的信息：书名；作者；价格；");
        Scanner sc=new Scanner(System.in);
        this.book[this.sz-1].name=sc.nextLine();
        this.book[this.sz-1].writer=sc.nextLine();
        this.book[this.sz-1].price=sc.nextInt();
    }
    public void exit(){
        System.exit(0);
    }
    public void add(){
       if(isFull())
       {
          this.book=kuoRong();
       }

        System.out.println("请输入新增书的信息：书名；作者；价格；");
        Scanner sc=new Scanner(System.in);
        this.book[this.sz]=new Book();
        this.book[this.sz].name=sc.nextLine();
        this.book[this.sz].writer=sc.nextLine();
        this.book[this.sz].price=sc.nextInt();
        this.sz++;
    }
    public boolean isFull(){
        if(this.sz==this.book.length){
            return true;
        }
        return false;
    }
    public Book[] kuoRong(){
       return Arrays.copyOf(this.book,book.length*2);

    }
}
